package com.aigc.sclassroom.mapper;

import com.aigc.sclassroom.pojo.SysUser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select u.user_id,dept_id,user_name,nick_name,email,phonenumber,sex,avatar,password,u.status,u.del_flag,role_name\n" +
            "from sys_user u\n" +
            "left join sys_user_role ur on u.user_id=ur.user_id\n" +
            "left join sys_role r on ur.role_id=r.role_id\n" +
            "where user_name=#{userName}")
    SysUser findByUserName(String username);

    @Insert("insert into sys_user(user_name,nick_name,password,create_time,update_time)"+"" +
            "values (#{userName},#{userName},#{encodePassword},now(),now())")
    void add(String userName, String encodePassword);

    @Insert("insert into sys_user_role(user_id,role_id)"+
            "values (#{userId},#{roleId})")
    void addUserRole(Long userId,Long roleId);

    @Select("select user_Name from sys_user where user_id=#{userId}")
    SysUser getUserNameById(Long userId);

    @Select("select role_key from sys_role join sys_user_role on sys_role.role_id=sys_user_role.role_id where user_id=#{userId}")
    String getRoleKeyByUserId(Long userId);

    @Select("select user_id from sys_user where user_name = #{userName}")
    Long getUserIdByName(String userName);

    @Select("select user_Name from sys_user where user_id=#{userId}")
    String getUsernNameById(Long teamUserId);

    @Update("update sys_user set nick_name=#{nickName},email=#{email},phonenumber=#{phonenumber},sex=#{sex} where user_id=#{userId}")
    void update(SysUser user);

    @Update("update sys_user set avatar=#{avatarUrl} where user_id=#{userId}")
    void updateAvatar(String avatarUrl, Long userId);

    @Update("update sys_user set password=#{newPwd} where user_id=#{userId}")
    void updatePwd(String newPwd, Long userId);

}
